package com.net.marketing.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.net.marketing.domain.MemberDetails;
import com.net.marketing.rowmapper.MemberDetailsRowMapper;
import com.net.marketing.statementcreator.InsertMemberDetailsStatementCreator;
import com.net.marketing.statementcreator.UpdateMemberDetailsStatementCreator;

/**
 *@Author pao quilala
 *@Date   Aug 10, 2012
 **/

@Component
public class JdbcMemberDetailsDao implements MemberDetailsDao {
	
	private JdbcTemplate jdbcTemplate;	

	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Transactional(readOnly = false)
	public int updateMemberDetails(MemberDetails memberDetails) {
		return this.jdbcTemplate.update(new UpdateMemberDetailsStatementCreator(memberDetails));		
	}

	@Transactional(readOnly = false)
	public int deleteMember(long memberId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Transactional(readOnly = true)
	public List<MemberDetails> getChildren(long memberId) {		
		return this.jdbcTemplate.query("SELECT * "+
				"  FROM member_details "+
				" START WITH sponsor_id ="+ memberId +
			   "CONNECT BY PRIOR member_id = sponsor_id",new MemberDetailsRowMapper());
	}

	@Transactional(readOnly = true)
	public MemberDetails getMemberDetails(long memberId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly = false)
	public int addMember(MemberDetails memberDetails) {
		return this.jdbcTemplate.update(new InsertMemberDetailsStatementCreator(memberDetails));		
	}

	public List<MemberDetails> getParent() {		
		return this.jdbcTemplate.query("SELECT * "+
				"  FROM member_details ",new MemberDetailsRowMapper());
	}

}
